﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace MisClient
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
        }


        private void btnLogin_Click(object sender, EventArgs e)
        {
            string _UserCode = this.txtUserCode.Text.Trim();
            string _DeptCode = this.txtDeptCode.Text.Trim();
            string _Pwd = this.txtPwd.Text.Trim();
            if (string.IsNullOrEmpty(_UserCode))
            {
                MessageBox.Show("人员编号不能为空!", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtUserCode.Focus();
                return;
            }
            if (string.IsNullOrEmpty(_DeptCode))
            {
                MessageBox.Show("部门编号不能为空!", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtDeptCode.Focus();
                return;
            }
            if (string.IsNullOrEmpty(_Pwd))
            {
                MessageBox.Show("口令不能为空!", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtPwd.Focus();
                return;
            }


            IBLL.IOper _Oper = MisCLL.DAOFactory.CreateInstance().GetRemoteOper();
            DataTable dt = _Oper.GetOper(_UserCode, _DeptCode,_Pwd);
            if (dt == null || dt.Rows.Count == 0)
            {
                MessageBox.Show("登录信息有错误，无法登录", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {

                var accountUser = new UserInfo { UserCode = _UserCode, Pass = _Pwd, DeptCode = _DeptCode, Oper = dt.Rows[0]["oper"].ToString() };
                UserInfo.SaveDisk(accountUser);
                this.Close();
            }
        }

        private void txtUserCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(this.txtUserCode.Text))
                {
                    MessageBox.Show("人员编号不能为空!", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtUserCode.Focus();
                }
                else
                {
                    this.txtDeptCode.Focus();
                }
            }
        }

        private void txtDeptCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(this.txtDeptCode.Text))
                {
                    MessageBox.Show("部门编号不能为空!", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtDeptCode.Focus();
                }
                else
                {
                    this.txtPwd.Focus();
                }
            }
        }

        private void txtPwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(this.txtDeptCode.Text))
                {
                    MessageBox.Show("口令不能为空!", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtPwd.Focus();
                }
                else
                {
                    this.btnLogin.Focus();
                }
            }
        }
    }
}
